﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.User
{
    public class LogEdit : YouHP.Pages.User.UserBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected GroupInfo GroupData;
        protected LogTopicInfo LogTopicData;

        protected void HandleCommon()
        {
            this.SetTitle("修改日志");
            this.AddLocation("修改日志", "/User/LogEdit.aspx");

            int lid = RequestTools.GetInt("lid", 0);
            if (lid <= 0)
            {
                this.AddErrLine("无效的参数 lid");
                return;
            }

            LogTopicData = LogTopicBLL.Instance.GetLogTopicInfo(lid);
            if (LogTopicData.Id <= 0 || LogTopicData.HostUserId != UserData.Id)
            {
                this.AddErrLine("日志不存在或您没权限编辑该主题");
                return;
            }
        }

        protected void HandlePost()
        {
            LogTopicInfo topic = ValidateForm();
            if (ErrorCount > 0) return;
            bool succ = LogTopicBLL.Instance.UpdateLogTopic(topic);

            PageKeys pageid = PageHelper.GetReferrerPageIdentify();

            string url = "/User/Log.aspx?id="+LogTopicData.Id;
            if (succ)
            {
                this.AddMsgLine("修改成功");
                this.SetUrl(url);
                this.SetMetaRefresh();
            }
            else
            {
                this.AddErrLine("修改失败");
                return;
            }

        }


        protected void HandleGet()
        {
            //this.AddScriptSrc(TinyMCE.Path);
            //this.AddScript(TinyMCE.InitAdvance("logbody"));
        }

        LogTopicInfo ValidateForm()
        {
            LogTopicInfo info = new LogTopicInfo();
            info.Title = RequestTools.GetFormString("ttitle").Trim();
            info.Body = RequestTools.GetFormString("textcontent").Trim();
            if (info.Title.Length == 0)
            {
                this.AddErrLine("标题不能为空！");
                return null;

            }
            if (info.Title.Length > Config.TitleMaxSize)
            {
                this.AddErrLine("标题长度不能超过" + Config.TitleMaxSize+"字符！");
                return null;
            }

            if (info.Body.Length == 0)
            {
                this.AddErrLine("内容不能为空!");
                return null;
            }
            else if (info.Body.Length > Config.TopicMaxSize)
            {
                this.AddErrLine("内容长度不能超过 " + Config.TopicMaxSize + "字符！");
                return null;
            }
            info.HostUserId = UserData.Id;
            info.UpdateTime = DateTime.Now;
            info.Id = LogTopicData.Id;

            return info;
        }
    }
}